package com.qsboss.demo.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.qsboss.demo.pojo.TbSysLog;
import com.qsboss.demo.pojo.TbUser;
import com.qsboss.demo.service.*;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricDetail;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @author 军哥
 * @version 1.0
 * @description: 根目录
 * @date 2023/10/22 13:12
 */

@Controller
@RequestMapping(value = "/")
public class IndexController {

    @Autowired
    HttpServletRequest httpServletRequest;

    @Autowired
    TbUserService tbUserService;

    @Autowired
    TbBillService tbBillService;

    @Autowired
    TbPromptService tbPromptService;

    @Autowired
    TbRepaymentService tbRepaymentService;

    @Autowired
    TbSysLogService tbSysLogService;

    @Autowired
    RepositoryService repositoryService;

    @Autowired
    RuntimeService runtimeService;

    @Autowired
    TaskService taskService;

    @Autowired
    HistoryService historyService;

    /***
     * @description 管理后台首页
     * @params
     * @return java.lang.String
     * @author 军哥
     * @date 2023/10/23 21:10
     */
    @GetMapping(value = "/index")
    public String index(Model model) {
        // 检查登录状态
        HttpSession session = httpServletRequest.getSession();
        TbUser user = (TbUser)session.getAttribute("user");
        if(user == null) {
            return "redirect:/login.do";
        }

        // 存放登录用户信息
        model.addAttribute("user", user);

        return "index";
    }

    /***
     * @description 登录页面
     * @params
     * @return java.lang.String
     * @author 军哥
     * @date 2023/10/23 21:10
     */
    @GetMapping(value = "/login")
    public String login() {
        return "login";
    }

    /***
     * @description 员工管理页面
     * @params model
     * @return java.lang.String
     * @author 军哥
     * @date 2023/10/23 21:10
     */
    @GetMapping(value = "/users")
    public String users(Model model, Integer pageNum, Integer pageSize) {

        if(pageNum==null || pageNum==0) {
            pageNum = 1;
        }
        if(pageSize==null || pageSize==0) {
            pageSize = 10;
        }

        PageInfo pageInfo = tbUserService.getUserByPage(pageNum, pageSize);

        List<TbUser> users = pageInfo.getList();

        model.addAttribute("users", users);
        model.addAttribute("pages", pageInfo.getPages());
        model.addAttribute("curPage", pageNum);
        model.addAttribute("pageSize", pageInfo.getPageSize());
        model.addAttribute("prePage", pageInfo.getPrePage());
        model.addAttribute("nextPage", pageInfo.getNextPage());
        model.addAttribute("navigatePageNums", pageInfo.getNavigatepageNums());

        return "users";
    }

    @GetMapping(value = "/sysLog")
    public String sysLog(Model model, Integer pageNum, Integer pageSize) {
        if(pageNum==null || pageNum==0) {
            pageNum = 1;
        }
        if(pageSize==null || pageSize==0) {
            pageSize = 10;
        }

        PageInfo pageInfo = tbSysLogService.getLogByPage(pageNum, pageSize);

        List<TbSysLog> list = pageInfo.getList();

        model.addAttribute("list", list);
        model.addAttribute("pages", pageInfo.getPages());
        model.addAttribute("curPage", pageNum);
        model.addAttribute("pageSize", pageInfo.getPageSize());
        model.addAttribute("prePage", pageInfo.getPrePage());
        model.addAttribute("nextPage", pageInfo.getNextPage());
        model.addAttribute("navigatePageNums", pageInfo.getNavigatepageNums());

        return "syslog";

    }

    @GetMapping(value = "/customer")
    public String customer(Model model, Integer pageNum, Integer pageSize) {
        if(pageNum==null || pageNum==0) {
            pageNum = 1;
        }
        if(pageSize==null || pageSize==0) {
            pageSize = 10;
        }

        PageInfo pageInfo = tbUserService.getCustomerByPage(pageNum, pageSize);

        List<TbUser> users = pageInfo.getList();

        model.addAttribute("users", users);
        model.addAttribute("pages", pageInfo.getPages());
        model.addAttribute("curPage", pageNum);
        model.addAttribute("pageSize", pageInfo.getPageSize());
        model.addAttribute("prePage", pageInfo.getPrePage());
        model.addAttribute("nextPage", pageInfo.getNextPage());
        model.addAttribute("navigatePageNums", pageInfo.getNavigatepageNums());

        return "customer";
    }

    /***
     * @description 显示账单列表页面
     * @params model
     * @params pageNum
     * @params pageSize
     * @return java.lang.String
     * @author 军哥
     * @date 2023/10/25 10:44
     */
    @GetMapping(value = "/bill")
    public String bill(Model model, Integer pageNum, Integer pageSize) {
        if(pageNum==null || pageNum==0) {
            pageNum = 1;
        }
        if(pageSize==null || pageSize==0) {
            pageSize = 10;
        }

        PageInfo pageInfo = tbBillService.getBillByPage(pageNum, pageSize);

        List list = pageInfo.getList();

        model.addAttribute("list", list);
        model.addAttribute("pages", pageInfo.getPages());
        model.addAttribute("curPage", pageNum);
        model.addAttribute("pageSize", pageInfo.getPageSize());
        model.addAttribute("prePage", pageInfo.getPrePage());
        model.addAttribute("nextPage", pageInfo.getNextPage());
        model.addAttribute("navigatePageNums", pageInfo.getNavigatepageNums());

        return "bill";
    }

    /***
     * @description 催款记录
     * @return java.lang.String
     * @author 军哥
     * @date 2023/10/25 10:47
     */
    @GetMapping(value = "/prompt")
    public String prompt(Model model, Integer pageNum, Integer pageSize) {
        if(pageNum==null || pageNum==0) {
            pageNum = 1;
        }
        if(pageSize==null || pageSize==0) {
            pageSize = 10;
        }

        PageInfo pageInfo = tbPromptService.getPromptByPage(pageNum, pageSize);

        List list = pageInfo.getList();

        model.addAttribute("list", list);
        model.addAttribute("pages", pageInfo.getPages());
        model.addAttribute("curPage", pageNum);
        model.addAttribute("pageSize", pageInfo.getPageSize());
        model.addAttribute("prePage", pageInfo.getPrePage());
        model.addAttribute("nextPage", pageInfo.getNextPage());
        model.addAttribute("navigatePageNums", pageInfo.getNavigatepageNums());

        return "prompt";
    }

    /***
     * @description 销账管理
     * @return java.lang.String
     * @author 军哥
     * @date 2023/10/25 14:55
     */
    @GetMapping(value = "/repay")
    public String repay(Model model, Integer pageNum, Integer pageSize) {
        if(pageNum==null || pageNum==0) {
            pageNum = 1;
        }
        if(pageSize==null || pageSize==0) {
            pageSize = 10;
        }

        PageInfo pageInfo = tbRepaymentService.getRepaymentByPage(pageNum, pageSize);

        List list = pageInfo.getList();

        model.addAttribute("list", list);
        model.addAttribute("pages", pageInfo.getPages());
        model.addAttribute("curPage", pageNum);
        model.addAttribute("pageSize", pageInfo.getPageSize());
        model.addAttribute("prePage", pageInfo.getPrePage());
        model.addAttribute("nextPage", pageInfo.getNextPage());
        model.addAttribute("navigatePageNums", pageInfo.getNavigatepageNums());

        return "repay";
    }

    /***
     * @description 我的账单页面
     * @return java.lang.String
     * @author 军哥
     * @date 2023/10/25 15:29
     */
    @GetMapping(value = "/mybill")
    public String pay(Model model, Integer pageNum, Integer pageSize) {
        // 检查登录状态
        HttpSession session = httpServletRequest.getSession();
        TbUser user = (TbUser)session.getAttribute("user");
        if(user == null) {
            return "redirect:/login.do";
        }

        if(pageNum==null || pageNum==0) {
            pageNum = 1;
        }
        if(pageSize==null || pageSize==0) {
            pageSize = 10;
        }

        // 存放欠账信息
        PageInfo pageInfo = tbBillService.getBillInCustomerByPage(pageNum, pageSize, user.getUserId());

        List list = pageInfo.getList();

        model.addAttribute("list", list);
        model.addAttribute("pages", pageInfo.getPages());
        model.addAttribute("curPage", pageNum);
        model.addAttribute("pageSize", pageInfo.getPageSize());
        model.addAttribute("prePage", pageInfo.getPrePage());
        model.addAttribute("nextPage", pageInfo.getNextPage());
        model.addAttribute("navigatePageNums", pageInfo.getNavigatepageNums());

        return "mybill";
    }


    /***
     * @description 流程列表页面
     * @params
     * @return java.lang.String
     * @author 军哥
     * @date 2023/10/23 21:11
     */
    @GetMapping(value = "/bpmn")
    public String bpmn(Model model) {

        List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery().list();

        model.addAttribute("bpmns", list);

        return "bpmn";
    }

    /***
     * @description 流程审批
     * @return java.lang.String
     * @author 军哥
     * @date 2023/10/25 20:46
     */
    @GetMapping(value = "/task")
    public String task(Model model) {
        List<Task> list = taskService.createTaskQuery().list();

        model.addAttribute("tasks", list);

//        for (Task task : list) {
//            task.getProcessInstanceId()
//        }

        return "task";
    }

    @GetMapping(value = "/history")
    public String history(Model model) {
        List<HistoricProcessInstance> list = historyService.createHistoricProcessInstanceQuery().list();
        for (HistoricProcessInstance item : list) {
            System.out.println(item.toString());
            System.out.println(item.getBusinessKey());
            System.out.println(item.getId());
            System.out.println(item.getProcessDefinitionName());
            System.out.println(item.getProcessDefinitionKey());
            System.out.println(item.getStartTime());
            System.out.println(item.getEndTime());
            Map<String, Object> variables = item.getProcessVariables();
            System.out.println(JSON.toJSONString(variables));

            List<HistoricDetail> detailList = historyService.createHistoricDetailQuery().processInstanceId(item.getId()).list();
            for (HistoricDetail historicDetail : detailList) {
                System.out.println(JSON.toJSONString(historicDetail));
            }
        }

        model.addAttribute("list", list);

        return "history";
    }

}
